package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.query.MessageQueryObject;
import cn.wolfcode.trip.base.service.IMessageService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/messages")
public class SystemMessageController {
    @Autowired
    private IMessageService messageService;

    //更改已读未读状态
    @PutMapping(value = "/reads")
    public JsonResult changeState(MessageQueryObject qo){
        messageService.changeState(qo);
        return new JsonResult();
    }
    //查询信息未读的信息
    @GetMapping("/{authorId}/unRead")
    public PageInfo listUreadMsg(MessageQueryObject qo){
        qo.setPageSize(8);
        int unReadCount = messageService.countForType(qo);
        if(unReadCount == 0){
            qo.setState(-1);
        }
        qo.setOrderBy("m.sendTime desc");
        PageInfo pageInfo = messageService.listUread(qo);
        return pageInfo;
    }

    //计算未读状态信息的数量
    @GetMapping("/{authorId}/{state}/counts")
    public List<Map> countForMsg(MessageQueryObject qo){
        return messageService.countForMsg(qo);
    }
}
